﻿using M.Warehouse.API.Application.Command.PickingCommand;
using M.Warehouse.Domain;
using M.Warehouse.Domain.DTO;
using M.Warehouse.Repository.WarehouseInterface;
using M_ApiResult;
//using M_Ingredient.Repository.Interface;
using MediatR;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Newtonsoft.Json;
using System.ComponentModel.DataAnnotations;
using W.Common;

namespace M.Warehouse.API.Application.CommandHandlers.PickingHandlers
{
    public class PickingShowCommandHandlers : IRequestHandler<PickingShowCommand, List<PickingShowDTO>>
    {
        private readonly IPickingRepository _pickingRepository;
        //private readonly IIngredientRepository _ingredientRepository;
        public PickingShowCommandHandlers(IPickingRepository pickingRepository/*,IIngredientRepository ingredientRepository*/)
        {
            _pickingRepository = pickingRepository;
            //_ingredientRepository = ingredientRepository;
        }
        public async Task<List<PickingShowDTO>> Handle(PickingShowCommand request, CancellationToken cancellationToken)
        {
            //List<Ingredient> productInfos = await GetIngredient(1, 100);
            var plist = await _pickingRepository.GetAllAsync();
            //var ilist=await _ingredientRepository.GetAllAsync();
            var list = from p in plist
                       //join i in ilist on p.PickingId equals i.PickingId
                       select new PickingShowDTO
                       {
                           PickingId = p.PickingId,
                           PickingNo = p.PickingNo,
                           PickingName = p.PickingName,
                           PickingDate = p.PickingDate,
                           Picker = p.Picker,
                           TicketsName = p.TicketsName,
                           TicketsNo = p.TicketsNo,
                           TaskNo = p.TaskNo,
                           TaskName = p.TaskName,
                           ProcessRouteName = p.ProcessRouteName,
                           ProductionProcessesName = p.ProductionProcessesName,
                           PickingState = p.PickingState,
                           PickingRemark = p.PickingRemark,
                           CreatedBy = p.CreatedBy,
                           CreatedDate = p.CreatedDate,
                           UpdatedBy = p.UpdatedBy,
                           UpdatedDate = p.UpdatedDate,
                           DeletedBy = p.DeletedBy,
                           DeletedDate = p.DeletedDate,
                           IsDeleted = p.IsDeleted,
                           //PickingNum = i.PickingNum
                       };
            if (!string.IsNullOrEmpty(request.PickingNo))
            {
                list = list.Where(x => x.PickingNo.Contains(request.PickingNo));
            }
            if (request.PickingDate!=null)
            {
                list = list.Where(x => x.PickingDate == Convert.ToDateTime(request.PickingDate).AddDays(1));
            }
            if (request.PickingState != null)
            {
                list = list.Where(x => x.PickingState == request.PickingState);
            }
            return list.ToList();
        }

        ///// <summary>
        ///// 原料
        ///// </summary>
        ///// <param name="pageIndex"></param>
        ///// <param name="pageSize"></param>
        ///// <returns></returns>
        //public async Task<dynamic> GetIngredient(int pageIndex, int pageSize)
        //{
        //    string token = "";
        //    HttpClientWrapper cilent = new HttpClientWrapper(token);
        //    string storage = "http://10.223.8.16:8077/yuan/api/Ingredient/Show?Pageindex=" + pageIndex + "&Pagesize=" + pageSize;

        //    string postRepsonse = await cilent.GetAsync(storage);
        //    var json = JsonConvert.DeserializeObject<ApiResult<Ingredient>>(postRepsonse);
        //    return json.Data.list;
        //}
        //public class Ingredient
        //{
        //    public int IngredientId { get; set; }
        //    public string? Ingredientnumber { get; set; }
        //    public string? IngredientName { get; set; }
        //    public string? IngredientFen { get; set; }
        //    public string? size { get; set; }
        //    public string? specification { get; set; }

        //    public string? Rawmaterialproperties { get; set; }
        //    public string? unit { get; set; }
        //    public int? Num { get; set; }

        //    public string? IngredientType { get; set; }

        //    public string? State { get; set; }
        //    public int? IngredientNum { get; set; }
        //    public int? alarmDays { get; set; }
        //    public int? InventoryLimit { get; set; }
        //    public int? InventoryMinimum { get; set; }
        //    public string? Rawmateriallocation { get; set; }
        //    public DateTime? CreatedTimess { get; set; }

        //    public string? remark { get; set; }
        //    public string? images { get; set; }
        //    public int? PickingId { get; set; }// 领料Id
        //    public int? PickingNum { get; set; }//领料数量

        //    /// <summary>
        //    /// 逻辑状态
        //    /// </summary>
        //    public bool IsDeleted { get; set; } = false;
        //}
    }
}
